package com.oracle.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * jdk代理对象封装
 */
public class Jdk_Proxy {

    public static UserDao getJdkProxy(){
        //1.目标类
        UserDao userDao = new UserDaoImpl();
        //2.功能类
        MyAspect aspect = new MyAspect();
        //接口
        Class[] interfaces = {UserDao.class};
        //3.创建代理类  Proxy 帮你new 一个代理对象
        //1.获取当前这个接口的 ClassLoader 类的加载器
        //2.你要创建哪个接口的实现类啊
        //3.InvocationHandler 处理器帮你真正的new 对象了
        UserDao user =(UserDao) Proxy.newProxyInstance(UserDao.class.getClassLoader(), interfaces,
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        aspect.before();
                        Object object = method.invoke(userDao, args);
                        aspect.after();
                        return object;
                    }
                });
        return user;
    }







}
